<?php

function list_pelatihan(){
	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		'Materi pelatihan'=>array('style'=>'border-bottom:2px solid;width:25%'),   
		'Nama pelatihan'=>array('style'=>'border-bottom:2px solid;width:30%'),  
		'Tgl. Mulai'=>array('style'=>'border-bottom:2px solid;width:15%'),   
		'Dijadwalkan'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),  
		'Realisasi'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:5%'), 
	);
	$query = "SELECT * FROM jadwal_pelatihan a
	INNER JOIN materi_pelatihan b ON a.materi_pelatihan_id = b. materi_pelatihan_id 
	ORDER BY jadwal_pelatihan_id DESC";
	$result = my_query($query );
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =25;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 25; 
	while($ey = my_fetch_array($result)){
		$i++;
		$pesertas = count_peserta($ey['jadwal_pelatihan_id']);
		 
		$detailproperty = array(
						'href'=>'index.php?com='.$_GET['com'].'&task=detail&id='.$ey['jadwal_pelatihan_id'],						 
						'title'=>'Peserta'
				);
		$detail_property = button_icon( 'b_props.png' , $detailproperty  );

		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'lsv'=>    $ey['materi_pelatihan_label'], 
			'sa'=>    $ey['nama_pelatihan'], 
			'tgl'=>	date("d-m-Y" , strtotime($ey['tanggal_mulai'])),
			'l'=>   position_text_align($pesertas['jadwal'] , 'center') ,     
			'r'=>   position_text_align($pesertas['realisasi'] , 'center') ,     
			'operasi'=> position_text_align(  $detail_property , 'right'),  
		);
	}	
	$datas = table_rows($row);  
	 $paging = $kgPagerOBJ ->showPaging();
	return table_builder($header , $datas ,  5 , false ,$paging );
	 
}


function detail_peserta_pelatihan($pelatihan_id){
	my_set_code_js('

	function confirmDelete(id){
		var t = confirm(\'Yakin akan menghapus data ?\');
		if(t){
			location.href=\'index.php?com='.$_GET['com'].'&task=delete&id=\'+id;
		}
		return false;
	}
	');	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'NIK'=>array('style'=>'text-align:center;border-bottom:2px solid;width:15%'),  
		'Nama Karyawan'=>array('style'=>'text-align:center;border-bottom:2px solid;width:60%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);
	
	
	$query = "SELECT * FROM karyawan_pelatihan WHERE jadwal_pelatihan_id ={$pelatihan_id}";
 
	$result = my_query($query);
	$row = array();
	while($ey = my_fetch_array($result)){
		$i++; 
		$karyawan = loaddata_karyawan($ey['karyawan_id']); 
		$form = '<input type="checkbox" name="a['.$ey['karyawan_id'].']" value="1" '.( $ey['realisasi']=="Y" ? ' checked="checked" ': '').'/>';		

		$row[] = array(
		 	'#'=>position_text_align ($i, 'center'),
			'nik'=>    position_text_align($karyawan['nik'],'center') ,  
			'nama'=>   $karyawan['nama'],     
			'operasi'=> position_text_align ($form, 'right'), 
		);
	}	
	$datas = table_rows($row);  
	 
	$fd = "SELECT * FROM jadwal_pelatihan a 
	INNER JOIN kategori_pelatihan b ON a.kategori_pelatihan_id = b.kategori_pelatihan_id
	INNER JOIN materi_pelatihan  c ON a.materi_pelatihan_id = c.materi_pelatihan_id
	WHERE a.jadwal_pelatihan_id = {$pelatihan_id}";
	$re = my_query($fd);
	$wp = my_fetch_array($re);
	$view ='<table width="98%" cellspacing="0" cellpadding="4"><tr>'; 
	$view .= form_header( "detail pegawai" , "cp"  );
	$view .= form_field_display( '<span class="label_form">'.strtoupper($wp['kategori_pelatihan_label']).'</span>'   , "<b>Kategori pelatihan</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'.ucfirst($wp['materi_pelatihan_label']).'</span>'   , "<b>Materi pelatihan</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'.ucfirst($wp['nama_pelatihan']).'</span>'   , "<b>Nama pelatihan</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'. date('d - m - Y' , strtotime($wp['tanggal_mulai']) ) .' s/d '. date('d - m - Y' , strtotime($wp['tanggal_selesai']) ) .'</span>'   , "<b>Tanggal pelatihan</b>"    ); 
	$view .= '<tr><td><td><div style="width:100%;text-align:right;">
	<input type="button" value=" Kembali " class="main_button" onclick="javascript:location.href=\'index.php?com='.$_GET['com'].'\'"/>
	<input type="button" value=" Cetak " class="main_button" onclick="javascript:window.open(\'index.php?com='.$_GET['com'].'&task=cetak&id='.$pelatihan_id.'\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,resizable=0\');"/> 
	<input type="button" value="Submit" class="main_button" onclick="javascript:document.f.submit()" /></div></td></tr>'   ;  
	$view .= form_footer( );
	$view .= '</td></tr></table><br/>'; 
	return  $view . '<form method="POST" name="f">'.table_builder($header , $datas ,  5 , false );
}

function set_realisasi_pelatihan($id, $datas){
	my_query("UPDATE karyawan_pelatihan SET realisasi='T' WHERE jadwal_pelatihan_id = {$id} ");  
	foreach($datas as $key=>$data){
		my_query("UPDATE karyawan_pelatihan SET realisasi='Y' WHERE jadwal_pelatihan_id = {$id} AND karyawan_id={$key}");  
	}
	return true;
} 

function print_plain($pelatihan_id){
	$fd = "SELECT * FROM jadwal_pelatihan a 
	INNER JOIN kategori_pelatihan b ON a.kategori_pelatihan_id = b.kategori_pelatihan_id
	INNER JOIN materi_pelatihan  c ON a.materi_pelatihan_id = c.materi_pelatihan_id
	WHERE a.jadwal_pelatihan_id = {$pelatihan_id}";
	$re = my_query($fd);
	$wp = my_fetch_array($re);
	$datas = array();  
	$datas[] = array(
		 'k1'=>array("Kategori pelatihan" ,23 ,STR_PAD_RIGHT),
		 'k2'=>array(":",2,STR_PAD_RIGHT),	
		 'k3'=>array($wp['kategori_pelatihan_label'] ,75,STR_PAD_RIGHT),	
	); 	 
	$datas[] = array(
		 'k1'=>array("Materi pelatihan" ,23 ,STR_PAD_RIGHT),
		 'k2'=>array(":",2,STR_PAD_RIGHT),	
		 'k3'=>array($wp['materi_pelatihan_label'] ,75,STR_PAD_RIGHT),	
	); 	 
	$datas[] = array(
		 'k1'=>array("Nama pelatihan" ,23 ,STR_PAD_RIGHT),
		 'k2'=>array(":",2,STR_PAD_RIGHT),	
		 'k3'=>array($wp['nama_pelatihan'] ,75,STR_PAD_RIGHT),	
	); 	 
	$datas[] = array(
		 'k1'=>array("Tanggal pelatihan" ,23 ,STR_PAD_RIGHT),
		 'k2'=>array(":",2,STR_PAD_RIGHT),	
		 'k3'=>array(date('d - m - Y' , strtotime($wp['tanggal_mulai']) ) .' s/d '. date('d - m - Y' , strtotime($wp['tanggal_selesai']) ) ,75,STR_PAD_RIGHT),	
	); 
	$query = "SELECT * FROM karyawan_pelatihan WHERE jadwal_pelatihan_id ={$pelatihan_id} AND realisasi='Y'";
	$result = my_query($query);
	$i=1;
	$datas[] = array(
		 'k1'=>array(str_repeat("-", 100) ,100 ,STR_PAD_LEFT),
	); 
	$datas[] = array(
		 'k1'=>array("NO" ,5 ,STR_PAD_LEFT),
		 'k2'=>array("NIK" ,20,STR_PAD_RIGHT),
		 'k3'=>array("Nama Karyawan" ,75,STR_PAD_RIGHT),
	); 
	$datas[] = array(
		 'k1'=>array(str_repeat("-", 100) ,100 ,STR_PAD_LEFT),
	); 
	$i=0;
	while($ey = my_fetch_array($result)){
		if(	$ey['realisasi']=="Y" ){
			$i++;
			$karyawan =  loaddata_karyawan($ey['karyawan_id']);  
			$datas[] = array(
				 'k1'=>array($i ,5 ,STR_PAD_LEFT),
				 'k2'=>array($karyawan['nik'] ,20,STR_PAD_RIGHT),
				 'k3'=>array($karyawan['nama_gelar'] ,40,STR_PAD_RIGHT),
			); 		
		}			
	}
	$view = 	str_pad("CETAK REALISASI PELATIHAN",100," ",STR_PAD_BOTH);
	return $view ."\n"."\n".set_txt_report($datas);	
}

function count_peserta($pelatihan_id){
	$query = "SELECT realisasi FROM karyawan_pelatihan WHERE jadwal_pelatihan_id={$pelatihan_id}";
	$result = my_query($query);
	$jadwal=$realisasi=0 ;
	while($ey = my_fetch_array($result)){
		$jadwal++;
		if( $ey['realisasi']=="Y"){
			$realisasi++;
		}
	}	
	$datas['jadwal'] = $jadwal;
	$datas['realisasi'] = $realisasi;	
	return  $datas;
}
